// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import router from './router'
import VueCookies from 'vue-cookies'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import axios from 'axios'
import { getAPI } from './assets/js/resources.js'

axios.defaults.withCredentials = true
axios.interceptors.request.use(
  config => {
    let token = VueCookies.get('NIX_ANIM_USER_SESSION')
    if (token) {
      Object.assign(config.headers, { 'token': token })
    }
    return config
  },
  err => {
    return Promise.reject(err)
  }
);
axios.interceptors.response.use(
  res => {
    if (res.data.code === '401') {
      location.href = getAPI('URL_LOGIN')
      return false
    };
    return Promise.resolve(res)
  },
  error => {
    return Promise.reject(error)
  }
);
Vue.prototype.$axios = axios

Vue.config.productionTip = false

Vue.use(VueCookies)
Vue.use(ElementUI)

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
